﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InHD.utilities
{
    class Utilities
    {
        private static string[] ConvertStrArr = new string[] { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };

        public static string ReadVND(long roundedMoney)
        {
            string finalResult = string.Empty;

            long tmp = roundedMoney;
            List<long> tienTi = new List<long>();
            while (tmp > 0)
            {
                tienTi.Add((tmp % 1000000000));
                tmp = tmp / 1000000000;
            }
            for (int i = tienTi.Count - 1; i >= 0; i--)
            {
                List<long> tienTrieu = new List<long>();
                long tmp2 = tienTi[i];
                while (tmp2 > 0)
                {
                    tienTrieu.Add(tmp2 % 1000);
                    tmp2 /= 1000;
                }

                string result = ReadVND(tienTrieu, (i == tienTi.Count - 1) ? true : false);
                for (int j = 0; j < i; j++)
                {
                    result += " tỉ";
                }

                finalResult += result +" ";
            }

            finalResult = char.ToUpper(finalResult[0]) + finalResult.Substring(1);
            return finalResult + "Việt Nam đồng chẵn";
        }

        private static string ReadThreeDigits(long number, bool isStart)
        {
            long num1 = number % 10;
            number /= 10;
            long num2 = number % 10;
            number /= 10;
            long num3 = number % 10;
            number /= 10;
            string cNum1 = string.Empty;
            string cNum2 = string.Empty; 
            string cNum3 = string.Empty; 
            if (isStart)
            {
                cNum1 = num1 > 0 ? ConvertStrArr[num1] : string.Empty;
                cNum2 = num2 > 0 ? ConvertStrArr[num2] : (num1 > 0 && num3 > 0 ? "lẻ" : string.Empty);
                cNum3 = num3 > 0 ? ConvertStrArr[num3] : string.Empty;
                if (!string.IsNullOrEmpty(cNum3))
                {
                    cNum3 += " trăm";
                }

                if (num2 == 1)
                {
                    cNum2 = "mười";
                }
                else
                {
                    if (num2 > 0) cNum2 += " mươi";
                }
            }
            else
            {
                cNum1 = num1 > 0 ? ConvertStrArr[num1] : string.Empty;
                cNum2 = num2 > 0 ? ConvertStrArr[num2] : (num1 > 0 ? "lẻ" : string.Empty);
                cNum3 = num3 > 0 ? ConvertStrArr[num3] : ((string.IsNullOrEmpty(cNum1) && string.IsNullOrEmpty(cNum2)) ? string.Empty : ConvertStrArr[num3]);
                if (!string.IsNullOrEmpty(cNum3))
                {
                    cNum3 += " trăm";
                }

                if (num2 == 1)
                {
                    cNum2 = "mười";
                }
                else
                {
                    if (num2 > 0) cNum2 += " mươi";
                }

                if (num1 == 1)
                {
                    if (num2 == 1)
                    {
                        cNum1 = ConvertStrArr[num1];
                    }
                    else
                    {
                        cNum1 = "mốt";
                    }
                }
            }

            return (string.IsNullOrEmpty(cNum3) ? string.Empty : cNum3 + " ") +
                (string.IsNullOrEmpty(cNum2) ? string.Empty : cNum2 + " ") + 
                cNum1;
        }

        private static string ReadVND(List<long> money, bool isStart)
        {
            string trieu = money.Count >= 3 ? ReadThreeDigits(money[2], isStart) : string.Empty;
            string nghin = money.Count >= 2 ? ReadThreeDigits(money[1], string.IsNullOrEmpty(trieu) ? isStart : false) : string.Empty;
            string donvi = ReadThreeDigits(money[0], string.IsNullOrEmpty(trieu) && string.IsNullOrEmpty(nghin) ? isStart : false);
            if (!string.IsNullOrEmpty(trieu))
            {
                trieu += " triệu";
            }
            if (!string.IsNullOrEmpty(nghin))
            {
                nghin += " nghìn";
            }

            return (string.IsNullOrEmpty(trieu) ? string.Empty : trieu + " ") +
                (string.IsNullOrEmpty(nghin) ? string.Empty : nghin + " ") + 
                donvi;
        }
    }
}
